const Koa = require('koa');
const Router = require('koa-router');
const router = new Router();
const app = new Koa();

//get post put delete ...
//* 表示所有路径
//路径参数
router.get('/ssr/api/news',async (ctx,next) => {
  console.log('news');
  ctx.body = {
    data:[1, 2, 3, 4, 5]
  };
});

router.get('/ssr/api/isLogin',async (ctx,next) => {
  console.log('isLogin~~~~');
  // console.log('ctx.cookies.get(\'login\'):',ctx.cookies.get('login'));
  // console.log(typeof JSON.parse(ctx.cookies.get('login')));
  ctx.body = {
    data:{
      login: JSON.parse(ctx.cookies.get('login')||false)
    }
  };
});

router.get('/ssr/api/login',async (ctx,next) => {
  console.log('login');
  ctx.cookies.set('login', true);
  ctx.body = {
    data:{
      login: true
    }
  };
});

router.get('/ssr/api/logout',async (ctx,next) => {
  console.log('logout');
  ctx.cookies.set('login', false);
  ctx.body = {
    data:{
      login: false
    }
  };
});

router.get('/ssr/api/translate',async (ctx,next) => {
  console.log('translate');
  ctx.body = {
    success: true,
    data:[
      {
        id:1,
        title: 'aaaaaaaa'
      },
      {
        id:2,
        title: 'bbbbbbb'
      },
      {
        id:3,
        title: 'ccccccc'
      },
      {
        id:4,
        title: 'ddddddd'
      },
      {
        id:5,
        title: 'eeeeeee'
      }
    ]
  };
});


// 装载路由
app.use(router.routes());

app.listen(9999,()=>{
  console.log(`server is running on ${9999}`);
});
